package com.shop2cn.app.liveseller.testcase;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.shop2cn.app.liveseller.parameter.CreateSellerLiveBean;
import com.shop2cn.app.liveseller.parameter.SellerLiveListBean;
import com.shop2cn.app.liveseller.service.SellerLiveListCall;
import com.ymatou.iapi.godmch.parameter.setBizConfigBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.LiveRoomShop2cnCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.livesellerShop2cn.*;
import com.ymttest.database.sqlwapper.livesellerShop2cn.LiveRoomShop2cnWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 买手直播列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SellerLiveList {
	private static SellerLiveListBean sellerlivelistBean;
	private static SellerLiveListCall sellerlivelistCall;
	private static LiveRoomShop2cnWapper LiveRoomShop2cnWapper;
	private static int sellerId;
	private static String token;
	private static int csId;
	private static String cstoken;

	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("买手直播列表");
		sellerId = GodmchManageCallService.CreateSellerMch();
		token = AuthCallServiceV2.createToken(sellerId, "app");
		csId = GodmchManageCallService.bindClientService(sellerId, 1);
		cstoken = AuthCallServiceV2.createToken(csId, "app");
	}

	@Before
	public void caseUp() {
		sellerlivelistBean = new SellerLiveListBean();
		sellerlivelistCall = new SellerLiveListCall();
		LiveRoomShop2cnWapper = new LiveRoomShop2cnWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerlivelistCall.getOpurl());
		LiveRoomShop2cnWapper = null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveList_001() {
		Logger.start(true, "查询买手直播列表成功");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService
					.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(), sellerId);
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			String head = "accesstoken=" + token;
			Logger.debug("ymt-pars:" + head);
			sellerlivelistCall.addHeader("ymt-pars", head);
			sellerlivelistCall.callService();
			long count = LiveRoomShop2cnWapper.selectCountLiveroom(createlive.getUserId(), null);
			Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("totals").getAsLong(), "验证count");

			JsonArray ja = sellerlivelistCall.getJsonObject().get("liveList").getAsJsonArray();
			for (JsonElement jsonElement : ja) {
				Logger.verifyEquals(1,jsonElement.getAsJsonObject().get("liveType").getAsInt(), "验证liveType");
				Logger.debug("liveType:" + jsonElement.getAsJsonObject().get("liveType").getAsInt());
//				Logger.verifyNotNull(jsonElement.getAsJsonObject().get("roomId").getAsInt(), "验证roomId");
//				Logger.debug("roomId:" + jsonElement.getAsJsonObject().get("roomId").getAsInt());
				Logger.verifyNotNull(jsonElement.getAsJsonObject().get("buyerShow").getAsBoolean(), "验证buyerShow");
				Logger.debug("buyerShow:" + jsonElement.getAsJsonObject().get("buyerShow").getAsBoolean());
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	// 翻页操作太慢 略过
	public void Tc_SellerLiveList_002() {
		Logger.start(true, "翻页查询买手直播列表成功");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService
					.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(), sellerId);
			int index = 1;
			int size = 2;
			boolean hasnext = true;
			Set liveset = new HashSet<>();
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(index);
			sellerlivelistBean.setPageSize(size);
			sellerlivelistCall.setData(sellerlivelistBean);
			long count = LiveRoomShop2cnWapper.selectCountLiveroom(createlive.getUserId(), null);
			// 返回顺序需要按业务需求人工check
			while (hasnext) {
				sellerlivelistBean.setPageIndex(index);
				String head = "accesstoken=" + token;
				Logger.debug("ymt-pars:" + head);
				sellerlivelistCall.addHeader("ymt-pars", head);
				sellerlivelistCall.callService();
				Logger.verifyEquals(count,
						sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsLong(),
						"验证count");
				JsonArray ja = sellerlivelistCall.getJsonObject().get("liveList").getAsJsonArray();
				for (JsonElement jsonElement : ja) {
					Logger.verifyNotNull(jsonElement.getAsJsonObject().get("liveType").getAsInt(), "验证liveType");
					Logger.debug("liveType:" + jsonElement.getAsJsonObject().get("liveType").getAsInt());
					Logger.verifyNotNull(jsonElement.getAsJsonObject().get("roomId").getAsInt(), "验证roomId");
					Logger.debug("roomId:" + jsonElement.getAsJsonObject().get("roomId").getAsInt());
					Logger.verifyNotNull(jsonElement.getAsJsonObject().get("buyerShow").getAsBoolean(), "验证buyerShow");
					Logger.debug("buyerShow:" + jsonElement.getAsJsonObject().get("buyerShow").getAsInt());
					boolean b = liveset.add(jsonElement.getAsJsonObject().get("Id"));
					if (!b) {
						Logger.comment(jsonElement.getAsJsonObject().get("Id") + "重复");
					}
				}
				index++;
				if (liveset.size() >= count) {
					hasnext = false;
				}
			}
			Thread.sleep(1);
			Logger.verifyEquals(count, Long.valueOf(liveset.size()), "验证不重复直播数size");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SellerLiveList_003() {
		Logger.start(true, "不输入userid，可以用");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService
					.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(), sellerId);
			// sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			String head = "accesstoken=" + token;
			Logger.debug("ymt-pars:" + head);
			sellerlivelistCall.addHeader("ymt-pars", head);
			sellerlivelistCall.callService();
			long count = LiveRoomShop2cnWapper.selectCountLiveroom(createlive.getUserId(), null);
			Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("totals").getAsLong(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SellerLiveList_004() {
		Logger.start(true, "不输入pagesize返回空");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService
					.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(), sellerId);
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			// sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			String head = "accesstoken=" + token;
			Logger.debug("ymt-pars:" + head);
			sellerlivelistCall.addHeader("ymt-pars", head);
			sellerlivelistCall.callService();
			Logger.verifyEquals("0", sellerlivelistCall.getJsonObject().get("totals").getAsString(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SellerLiveList_005() {
		Logger.start(true, "不输入pageindex失败");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService
					.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(), sellerId);
			sellerlivelistBean.setUserId(createlive.getUserId());
			// sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			String head = "accesstoken=" + token;
			Logger.debug("ymt-pars:" + head);
			sellerlivelistCall.addHeader("ymt-pars", head);
			sellerlivelistCall.callService();
			Logger.verifyEquals("500", sellerlivelistCall.getString("code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveList_006() {
		Logger.start(true, "客服查询买手直播列表成功");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService
					.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(), sellerId);
//			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			String head = "accesstoken=" + cstoken;
			Logger.debug("ymt-pars:" + head);
			sellerlivelistCall.addHeader("ymt-pars", head);
			sellerlivelistCall.callService();
			long count = LiveRoomShop2cnWapper.selectCountLiveroom(createlive.getUserId(), null);
			Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("totals").getAsLong(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveList_007() {
		Logger.start(true, "查询买手直播列表成功-BuyerShow");
		try {
			sellerId = GodmchManageCallService.CreateSellerMch();
			token=AuthCallServiceV2.createToken(sellerId, "app");
			
			//livetype 1tx同步过来的需要真实数据
			setBizConfigBean bizbean=GodmchManageCallService.initSetBizConfig("live_broadcast_type","5");
			GodmchManageCallService.setBizConfig(bizbean);
			GodmchManageCallService.SetTrueMchChargeConfig(sellerId, Arrays.asList(0,1,2,3,4,5),true);
			GodmchManageCallService.ApplyCharge(sellerId, 5, 7);	
			
			CreateSellerLiveBean createsellerliveBean = LiveRoomShop2cnCallService.initCreatesellerliveBean();
			createsellerliveBean.getLiveDto().setBuyerShow(false);
			createsellerliveBean.setRequestId(TestDataManager.getRandomUUID().toString());
			LiveRoom createlive = LiveRoomShop2cnCallService
					.CreateSellerLive(createsellerliveBean, sellerId);
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			String head = "accesstoken=" + token;
			Logger.debug("ymt-pars:" + head);
			sellerlivelistCall.addHeader("ymt-pars", head);
			sellerlivelistCall.callService();
			long count = LiveRoomShop2cnWapper.selectCountLiveroom(createlive.getUserId(), null);
			Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("totals").getAsLong(), "验证count");

			JsonArray ja = sellerlivelistCall.getJsonObject().get("liveList").getAsJsonArray();
			for (JsonElement jsonElement : ja) {
				Logger.verifyEquals(1,jsonElement.getAsJsonObject().get("liveType").getAsInt(), "验证liveType");
//				Logger.verifyNotNull(jsonElement.getAsJsonObject().get("roomId").getAsInt(), "验证roomId");
//				Logger.debug("roomId:" + jsonElement.getAsJsonObject().get("roomId").getAsInt());
				Logger.verifyEquals(false,jsonElement.getAsJsonObject().get("buyerShow").getAsBoolean(), "验证buyerShow");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	// @Test
	// @Category({ P1.class })
	// @TestCase
	// public void Tc_SellerLiveList_006() {
	// //20180508 新需求 显示删除
	// Logger.start(true, "查询买手直播列表成功删除列表显示");
	// try {
	// LiveRoom createlive =
	// LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
	// sellerlivelistBean.setUserId(createlive.getUserId());
	// sellerlivelistBean.setPageIndex(1);
	// sellerlivelistBean.setPageSize(10);
	// sellerlivelistCall.setData(sellerlivelistBean);
	// String head="accesstoken="+token;Logger.debug("ymt-pars:"+
	// head);sellerlivelistCall.addHeader("ymt-pars",
	// head);sellerlivelistCall.callService();
	// Logger.verifyEquals("200", sellerlivelistCall.getString("Code"),
	// "验证Code");
	// long count =
	// LiveRoomShop2cnWapper.selectCountLiveroom(createlive.getUserId(),null);
	// Logger.verifyEquals(count,
	// sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsLong(),
	// "验证count");
	//
	// Logger.comment("新发一个直播然后关闭删除,查询删除显示");
	// createlive =
	// LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
	// LiveRoomShop2cnCallService.SellerLiveOnline(createlive.getId(),
	// createlive.getUserId());
	// LiveRoomShop2cnCallService.CloseSellerLive(createlive.getId(),
	// createlive.getUserId(), 8);
	// LiveRoomShop2cnCallService.RemoveSellerLive(createlive.getId(),
	// createlive.getUserId());
	// String head="accesstoken="+token;Logger.debug("ymt-pars:"+
	// head);sellerlivelistCall.addHeader("ymt-pars",
	// head);sellerlivelistCall.callService();
	// Logger.verifyEquals("200", sellerlivelistCall.getString("Code"),
	// "验证Code");
	// Logger.verifyEquals(count+1,
	// sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsLong(),
	// "验证count");
	//
	// } catch (Exception e) {
	// Logger.fail(e);
	// }
	// }
}